/**
 * UVa 101 The Block Problem
 * Author: chchwy
 * Last Modified: 2011.06.16
 * http://chchwy.github.com
 */
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;

struct Stack {
    int block[25];
    int height;
    Stack() { height = 0; }
    void push(int x) { block[height] = x; ++height; }
    int pop(int x) { return block[--height]; }
    int top(int x) { return block[height - 1]; }
};

Stack stack[25];  // the blocks
int position[25];   // current position of each block

void init_blocks(int n) {

    for (int i=0; i<n; ++i) {
        stack[i].push(i);
        position[i] = i;
    }
}

bool in_the_same_stack(int x,int y) {
    return (position[x] == position[y]);
}

// clear all the blocks stacked on 'x'
void clear_blocks_on(int x) {

    Stack theXStack = &stack[ position[x] ];

    while (stack[ position[x] ] != x) {

        // move block 'num' to its original stack[num]
        int num = stack[ position[x] ][ height[x] ];
        stack[num][ height[num] ] = num;
        height[num]++;


        stk[num].push_back(num);
        pos[num] = num;

        stk[px].pop_back();
    }
}

void move(int x, int y) {

    int px = pos[x], py = pos[y];

    vector<int>::iterator move_begin, move_end;

    move_begin = find( stk[px].begin(), stk[px].end(), x);
    move_end   = stk[px].end();

    // append blocks (move_begin ~ move_end) to the end of stk[pos[y]]
    stk[py].insert( stk[py].end(), move_begin, move_end );

    //update pos array
    for(list<int>::iterator it=move_begin; it!=move_end; ++it) {
        pos[ (*it) ] = py;
    }
    stk[px].erase( move_begin, move_end );
}

void print_result(int num_blocks) {

    for (int i=0; i<num_blocks; ++i) {
        printf("%d:",i);

        for (int j=0; j<stk[i].size(); ++j)
            printf(" %d", stk[i][j] );
        putchar('\n');
    }
}

int main() {

#ifndef ONLINE_JUDGE
    freopen("101.in","r",stdin);
#endif

    int num_blocks;
    cin >> num_blocks;
    init_blocks(num_blocks);

    string cmd1, cmd2;
    int a,b;

    // [move/pile] a [onto/over] b
    while (cin >> cmd1 >> a >> cmd2 >> b) {

        // ignore illegal command
        if (a == b) continue;
        if (in_the_same_stack(a, b)) continue;

        if(cmd1 == "move")
            clear_blocks_on(a);

        if(cmd2 == "onto")
            clear_blocks_on(b);

        move(a,b);
    }
    print_result(num_blocks);

    return 0;
}
